package cn.xhy.material.controller.admin.specifications;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.xhy.material.controller.admin.specifications.vo.SpecificationsPageReqVO;
import cn.xhy.material.controller.admin.specifications.vo.SpecificationsRespVO;
import cn.xhy.material.controller.admin.specifications.vo.SpecificationsSaveReqVO;
import cn.xhy.material.dal.dataobject.specifications.SpecificationsDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.xhy.material.service.specifications.SpecificationsService;
@Tag(name = "管理后台 - 规格")
@RestController
@RequestMapping("/material/specifications")
@Validated
public class SpecificationsController {

    @Resource
    private SpecificationsService specificationsService;

    @PostMapping("/create")
    @Operation(summary = "创建规格")
    @PreAuthorize("@ss.hasPermission('material:specifications:create')")
    public CommonResult<Integer> createSpecifications(@Valid @RequestBody SpecificationsSaveReqVO createReqVO) {
        return success(specificationsService.createSpecifications(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新规格")
    @PreAuthorize("@ss.hasPermission('material:specifications:update')")
    public CommonResult<Boolean> updateSpecifications(@Valid @RequestBody SpecificationsSaveReqVO updateReqVO) {
        specificationsService.updateSpecifications(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除规格")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('material:specifications:delete')")
    public CommonResult<Boolean> deleteSpecifications(@RequestParam("id") Integer id) {
        specificationsService.deleteSpecifications(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得规格")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('material:specifications:query')")
    public CommonResult<SpecificationsRespVO> getSpecifications(@RequestParam("id") Integer id) {
        SpecificationsDO specifications = specificationsService.getSpecifications(id);
        return success(BeanUtils.toBean(specifications, SpecificationsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得规格分页")
    @PreAuthorize("@ss.hasPermission('material:specifications:query')")
    public CommonResult<PageResult<SpecificationsRespVO>> getSpecificationsPage(@Valid SpecificationsPageReqVO pageReqVO) {
        PageResult<SpecificationsDO> pageResult = specificationsService.getSpecificationsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SpecificationsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出规格 Excel")
    @PreAuthorize("@ss.hasPermission('material:specifications:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSpecificationsExcel(@Valid SpecificationsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SpecificationsDO> list = specificationsService.getSpecificationsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "规格.xls", "数据", SpecificationsRespVO.class,
                        BeanUtils.toBean(list, SpecificationsRespVO.class));
    }

}
